<?php

class FS{
	
	/**
	 * 创建多级目录
	 *
	 * @param string $dir
	 * @return bool
	 */
	public static function mkDir($dir) {
		if (empty ( $dir )) {
			return false;
		}
		if (is_dir ( $dir )) {
			return true;
		}
		if (FS::mkDir ( dirname ( $dir ) )) {
			$mk = mkdir ( $dir );
			FS::chmod ( $dir );
			return is_dir ( $dir );
		} else {
		}
	}
	
	/**
	 * 改变文件或目录属性
	 *
	 * @param string $file
	 * @return bool
	 */
	public static function chmod($file) {
		chmod ( $file, 0777 );
		umask ( umask ( 0 ) );
	}
	
	/**
	 * 上传文件
	 *
	 * @param string $src
	 * @param string $des
	 * @return bool
	 */
	public static function upload($src, $des) {
		if (is_uploaded_file ( $src )) {
			FS::mkdir ( dirname ( $des ) );
			@move_uploaded_file ( $src, $des ) || @copy ( $src, $des );
			FS::chmod ( $des );
			return is_file ( $des );
		} else {
		}
	}
	
	/**
	 * 移动文件
	 *
	 * @param string $src
	 * @param string $des
	 * @return bool
	 */
	public static function move($src, $des) {
		if (file_exists ( $$des ))
			FS::mkdir ( dirname ( $des ) );
		@rename ( $src, $des );
		FS::chmod ( $des );
		return is_file ( $des );
	}
	
	/**
	 * 删除文件目录、目录下所有文件或删除文件
	 *
	 * @param string $fname
	 * @return bool
	 */
	public static function rm($fname) {
		if (is_array ( $fname )) {
			foreach ( $fname as $fval ) {
				FS::rm ( $fval );
			}
			return TRUE;
		} elseif (is_file ( $fname )) {
			return @unlink ( $fname ) ? true : false;
		} elseif (is_dir ( $fname )) {
			$handle = opendir ( $fname );
			while ( ($file = readdir ( $handle )) !== false ) {
				if ($file != '.' && $file != '..') {
					$dir = $fname . DIRECTORY_SEPARATOR . $file;
					is_dir ( $dir ) ? FS::rm ( $dir ) : @unlink ( $dir );
				}
			}
			closedir ( $handle );
			return @rmdir ( $fname ) ? true : false;
		}
	}
	
	/**
	 * 生成文件
	 *
	 * @param string $fname
	 * @param string $content
	 * @return bool
	 */
	public static function fwrite($fname, $content) {
		FS::mkdir ( dirname ( $fname ) );
		file_put_contents ( $fname, $content );
		FS::chmod ( $fname );
		return is_file ( $fname );
	}
	
	/**
	 * 追加内容到文件
	 *
	 * @param string $fname
	 * @param string $content
	 * @return bool
	 */
	public static function append($fname, $content) {
		FS::mkdir ( dirname ( $fname ) );
		$fp = fopen ( $fname, "a+" );
		fwrite ( $fp, $content );
		fclose ( $fp );
		FS::chmod ( $fname );
		return is_file ( $fname );
	}
	
	/**
	 * 读取文件
	 *
	 * @param string $fname
	 * @return sring
	 */
	public static function fread($fname) {
		if (is_file ( $fname )) {
			return file_get_contents ( $fname );
		} else {
		}
	}

	/**
	 * 获取目录
	 *
	 * @param string $baseDir 基础目录
	 * @param int $loop 目录级数
	 * @param mixed $unGetFiles 不获取的文件名列表
	 * @return string
	 */
	public static function getDir($baseDir, $loop = 1, $unGetDir = array()) {
		if (! is_dir ( $baseDir )) {
			return FALSE;
		}
		if ($unGetDir && ! is_array ( $unGetDir )) {
			$unGetDir = array ($unGetDir );
		}
		$dataArr = array ();
		-- $loop;
		$d = dir ( $baseDir );
		while ( ($entry = $d->read ()) !== false ) {
			$filename = $baseDir . '/' . $entry;
			if ('.' != $entry && '..' != $entry && is_dir ( $filename ) && (! $unGetDir || (! in_array ( $filename, $unGetDir )))) {
				$dataArr [] = $filename;
				if (0 < $loop) {
					$myDataArr = FS::getDir ( $filename, $loop, $unGetFiles );
					if ($myDataArr) {
						$dataArr = array_merge ( $dataArr, $myDataArr );
						unset ( $myDataArr );
					}
				}
			}
		}
		$d->close ();
		return $dataArr;
	}

	/**
	 * 读取目录文件
	 *
	 * @param string $dir
	 * @return array
	 */
	public static function getDirTree($dir) {
		$array = array();
		if(is_dir($dir))
		{
			$d = dir($dir);
			while (false !== ($entry = $d->read())) {
				if($entry!='.' && $entry!='..') {
					$tname = $dir.'/'.$entry;
					if(is_dir($tname)) {
						$array = array_merge($array, self::getDirTree($tname));
					}
					else
					{
						$entry = iconv('GBK', 'UTF-8//TRANSLIT', $entry);
						$tname = iconv('GBK', 'UTF-8//TRANSLIT', $tname);
						$array[] = array('fname'=>$entry, 'tname'=>$tname);
					}
				}
			}
			$d->close();
		}
		return $array;
	}
}

?>
